package com.ruoyi.web.controller.job;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.client.MovieClient;
import com.ruoyi.common.constant.MovieConstant;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.FilmPermitInfo;
import com.ruoyi.system.domain.vo.movie.FilmPermitInfoDetailVo;
import com.ruoyi.system.domain.vo.movie.FilmPermitInfoVo;
import com.ruoyi.system.mapper.FilmPermitInfoMapper;
import com.ruoyi.system.service.impl.FilmPermitInfoServiceImpl;
import com.ruoyi.web.controller.app.HandleRecordsController;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.internal.IgnoreForbiddenApisErrors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName FilmPermitInfoTask
 * @Author YangFu
 * @Date 2024/3/23 14:51
 * @Version 1.0
 */
@Slf4j
@Component
@EnableScheduling
@RequiredArgsConstructor
public class FilmPermitInfoTask {


    private final FilmPermitInfoServiceImpl filmPermitInfoService;

    private final MovieClient movieClient;

    private final RedisCache redisCache;

    //从国家电影局网站-同步电影公映许可证公示
    //5分钟执行一次
//     @Scheduled(cron = "0 0/5 * * * ?")
    //   @Async
    //@Scheduled(fixedRate = 10000) // 10秒
    public void getFilmPermitInfo() {

        Integer moviePage = redisCache.getCacheObject("movie_page");
        int page;
        if (Objects.isNull(moviePage)) {
            redisCache.setCacheObject("movie_page", 1);
            page = 1;
        } else {
            page = moviePage;
        }
        System.out.println("开始了===" + page);
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("size", 20);
        map.put("channelid", 237314);
        String url = MovieConstant.FILM_PERMIT_INFO_URL;
        R r = movieClient.postRequest(url, map, FilmPermitInfoVo.class);
        FilmPermitInfoVo data = (FilmPermitInfoVo) r.getData();
        if (Objects.isNull(data)) return;
        Integer total = data.getTotal();
        // page = page * 20 < total ? page + 1 : 1;
        if (page * 20 < total) {
            redisCache.setCacheObject("movie_page", page + 1);
        } else {
            return;
        }
        System.out.println("数据=====" + JSON.toJSONString(data));
        List<FilmPermitInfoDetailVo> detailVos = data.getData();
        List<FilmPermitInfo> filmPermitInfos = new ArrayList<>();
        for (FilmPermitInfoDetailVo detailVo : detailVos) {
            FilmPermitInfo filmPermitInfo = new FilmPermitInfo();
            filmPermitInfo.setTitle(detailVo.getPmin());
            filmPermitInfo.setPermitNumber(detailVo.getGyzh());
            filmPermitInfo.setCode(detailVo.getBima());
            filmPermitInfo.setProductionUnit(detailVo.getCpdw());
            filmPermitInfo.setAnnouncementDate(new Date());
            filmPermitInfos.add(filmPermitInfo);
        }
        filmPermitInfoService.saveBatch(filmPermitInfos);
    }
}
